package com.Ip.Controller;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.naming.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.Ip.service.UserService;
@Controller
@RequestMapping("/con")
public class MyController {
	@Autowired
	@Qualifier("userService")
	UserService userService;

	@RequestMapping("/add")
	public String add(){
		System.out.println("add");
		return "upload";
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(@RequestParam MultipartFile file,HttpServletRequest request,ModelMap model) {  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = file.getOriginalFilename();  
        System.out.println(path);  
        File targetFile = new File(path, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);  
        return "aa";  
    } 
	
	@RequestMapping("/test2")
	public ModelAndView test2(){
		ModelAndView m = new ModelAndView();
		m.setViewName("index");
		m.addObject("hi", "hello");
		return m;
	}
	@RequestMapping("/test3")
	public String test3(Map<String,Object> map){
		map.put("hi", "hello");
		return "aa";
	}
	@RequestMapping("/test4")
	public String test4(ModelMap map){
		map.addAttribute("hi", "hello");
		return "aa";
	}
	@RequestMapping("/test5")
	public String test5(){
		userService.add();
		return "aa";
	}
}
